package com.whfc.wxmp.api.fse.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.wxmp.annotation.CurrentWxUser;
import com.whfc.fse.dto.FseGantryDTO;
import com.whfc.fse.dto.FseGantryDataDTO;
import com.whfc.fse.dto.FseGantryDataLogDTO;
import com.whfc.fse.param.FseGantryAddParam;
import com.whfc.fse.param.FseGantryEditParam;
import com.whfc.fuum.entity.WxUser;

import com.whfc.wxmp.api.fse.service.WxFseGantryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @Description 龙门吊
 * @Author hw
 * @Date 2021-06-01 17:38
 * @Version 1.0
 */
@RestController
@RequestMapping("/mp/api/gantry")
public class WxFseGantryController extends BaseController {

    @Autowired
    private WxFseGantryService mxFseGantryService;

    /**
     * 2.0 龙门吊列表
     * @param user
     * @param deptId
     * @param keyword
     * @param bindFlag
     * @param netState
     * @return
     */
    @GetMapping(value = "/list")
    public Result list(@CurrentWxUser WxUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam(value = "keyword", required = false) String keyword,
                       @RequestParam(value = "bindFlag", required = false) Integer bindFlag,
                       @RequestParam(value = "netState", required = false) Integer netState) {
        logger.info("龙门吊列表(分页),loginer:{},deptId:{},keyword:{},bindFlag:{},netState:{}",
                user.getName(), deptId, keyword, bindFlag, netState);
        ListData<FseGantryDTO> pageData = mxFseGantryService.list(deptId, keyword, bindFlag, netState);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/details")
    public Result details(@CurrentWxUser WxUser user, @RequestParam("gantryId") Integer gantryId) {
        logger.info("龙门吊详情,loginer:{},gantryId:{}", user.getName(), gantryId);
        FseGantryDTO data = mxFseGantryService.details(gantryId);
        return ResultUtil.success(data);
    }

    @RequiresPermissions("fse/gantry/device/add")
    @PostMapping("add")
    public Result add(@CurrentWxUser WxUser user, @RequestBody @Validated FseGantryAddParam param) {
        logger.info("添加龙门吊,loginer:{},param:{}", user.getName(), param);
        mxFseGantryService.add(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("fse/gantry/device/edit")
    @PostMapping("edit")
    public Result edit(@CurrentWxUser WxUser user, @RequestBody @Validated FseGantryEditParam param) {
        logger.info("修改龙门吊,loginer:{},param:{}", user.getName(), param);
        mxFseGantryService.edit(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("fse/gantry/device/del")
    @PostMapping("/del/{gantryId}")
    public Result del(@CurrentWxUser WxUser user, @PathVariable("gantryId") Integer gantryId) {
        logger.info("删除龙门吊,loginer:{},gantryId:{}", user.getName(), gantryId);
        mxFseGantryService.del(gantryId);
        return ResultUtil.success();
    }


    @GetMapping(value = "/monitor")
    public Result monitor(@CurrentWxUser WxUser user,
                          @RequestParam("gantryId") Integer gantryId) {
        logger.info("龙门吊监测,loginer:{},bridgeId:{}",
                user.getName(), gantryId);
        FseGantryDataDTO data = mxFseGantryService.monitor(gantryId);
        return ResultUtil.success(data);
    }

    @GetMapping(value = "/gantryLog")
    public Result gantryLog(@CurrentWxUser WxUser user,
                            @RequestParam("gantryId") Integer gantryId,
                            @RequestParam("time") @DateTimeFormat(pattern = "yyyy-MM-dd") Date time) {
        logger.info("龙门吊历史记录,loginer:{},gantryId:{},time:{}",
                user.getName(), gantryId, time);
        ListData<FseGantryDataLogDTO> list = mxFseGantryService.gantryLog(gantryId, time);
        return ResultUtil.success(list);
    }
}
